﻿using Inovout.Locations;
using Inovout.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Store.Web.Controllers
{
    public class LocationController : KJBaseController<Locations.Location>
    {

        private ILocationService locationService;
        public LocationController(ILocationService locationService)
        {
            this.locationService = locationService;     
        }

        [HttpGet]
        public JsonResult GetAllProvices()
        {

            Location  location = locationService.FindById(142);//142: 中国
            List<Location> locationList = locationService.FindByParent(location).ToList();

            var data = from c in locationList
                       select new { LocationProvince = c.Id, LocationProvinceName = c.Name };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetAllCities(int locaionId)
        {
            Location location=null;
            if(locaionId!=0)
            {
                location = locationService.FindById(locaionId);
            }
            List<Location> locationList = locationService.FindByParent(location).ToList();

            var data = from c in locationList
                       select new { LocationCity = c.Id, LocationCityName = c.Name };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetAllRegions(int locaionId)
        {
            Location location = null;
            if (locaionId != 0)
            {
                location = locationService.FindById(locaionId);
            }
            List<Location> locationList = locationService.FindByParent(location).ToList();

            var data = from c in locationList
                       select new { LocationRegion = c.Id, LocationRegionName = c.Name };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

       

         
    }
}